package com.mdp.workflow.pub.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdp.core.service.BaseService;
import com.mdp.workflow.pub.entity.SysUser;
import com.mdp.workflow.pub.mapper.SysUserMapper;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 父类已经支持增删改查操作,因此,即使本类什么也不写,也已经可以满足一般的增删改查操作了.<br> 
 * 组织 com.qqkj  顶级模块 oa 大模块 hr.user 小模块 <br>
 * 实体 SysUser 表 ADM.sys_user 当前主键(包括多主键): userid;
 *
 * 如果多数据源切换有问题，看这便文章 https://blog.csdn.net/weixin_42556307/article/details/136486514
 ***/
@DS("adm-ds")
@Service("mdp.workflow.pub.sysUserService")
public class SysUserService extends BaseService<SysUserMapper, SysUser> {


    
    public List<Map<String,Object>> selectMdpUsersByFilter(String branchId, String filter) {
        Map<String,Object> user=new HashMap<>();
        user.put("branchId", branchId);
        user.put("filter", "%"+filter+"%");
        return baseMapper.selectMdpUsersByFilter( user);
    }
    
    public List<String> selectUsersByGroups(List<String> groups) {
        return baseMapper.selectUsersByGroups( groups);
    }
    /** 请在此类添加自定义函数 */


    
    public List<Map<String, Object>> selectMdpGroupsByFilter(String branchId, String filter) {
        Map<String,Object> p=new HashMap<>();
        p.put("branchId", branchId);
        p.put("filter", "%"+filter+"%");
        return baseMapper.selectMdpGroupsByFilter(p);
    }

    /**
     * 查询员工信息
     * @param sysUser
     * @return
     */
    
    public Map<String, Object> selectUserInfomation(Map<String, Object> sysUser) {
        return baseMapper.selectUserInfomation(sysUser);
    }

    /**
     * 查询员工姓名、性别、头像信息
     * @return
     */
    
    public List<Map<String, Object>> selectUserListForSimpleInfoByUserids(List<String> userids) {
        return baseMapper.selectUserListForSimpleInfoByUserids(userids);
    }




    /**
     * 查询员工姓名、性别、头像信息
     * @return
     */
    
    public List<Map<String, Object>> selectDeptListByDeptids(List<String> userids) {
        return baseMapper.selectDeptListByDeptids(userids);
    }

    /**
     * 查询员工姓名、性别、头像信息
     * @return
     */
    
    public List<Map<String, Object>> selectUserDeptsByUseridAndDeptidList(List<Map<String,Object>> useridAndDeptidList) {
        Map<String,String> result=new HashMap<>();
        Map<String,Map<String,Object>> map=new HashMap<>();

        if(useridAndDeptidList==null || useridAndDeptidList.size()==0){
            return new ArrayList<>();
        }
        return baseMapper.selectUserDeptsByUseridAndDeptidList(useridAndDeptidList);
    }


    /**
     * 查询员工姓名、性别、头像信息
     * @return
     */
    
    public List<Map<String, Object>> selectPostListByPostIds(List<String> userids) {
        return baseMapper.selectPostListByPostIds(userids);
    }
    /**
     * 查询员工姓名、性别、头像信息
     * @return
     */
    
    public Map<String, String> selectDeptMapForSimpleInfoByDeptids(List<String> deptids) {
        Map<String,String> result=new HashMap<>();
        if(deptids==null || deptids.size()==0){
            return result;
        }
        List<Map<String, Object>> deptidList= this.selectDeptListByDeptids(deptids);
        if(deptidList==null||deptidList.size()==0){
            return result;
        }else{
            for (Map<String, Object> map : deptidList) {
                result.put((String) map.get("deptid"),(String)map.get("deptName"));
            }
        }
        return result;
    }


    /**
     * 查询员工姓名、性别、头像信息
     * @return
     */
    
    public Map<String, String> selectPostMapForSimpleInfoByPostIds(List<String> postIds) {
        Map<String,String> result=new HashMap<>();
        if(postIds==null || postIds.size()==0){
            return result;
        }
        List<Map<String, Object>> postIdList= this.selectPostListByPostIds(postIds);

        if(postIdList==null||postIdList.size()==0){
            return result;
        }else{
            for (Map<String, Object> map : postIdList) {
                result.put((String) map.get("id"),(String)map.get("postName"));
            }
        }
        return result;
    }
    /**
     * 查询员工姓名、性别、头像信息
     * @return
     */
    
    public Map<String,String> selectUserMapForSimpleInfoByUserids(List<String> userids) {
        Map<String,String> result=new HashMap<>();
        if(userids==null||userids.size()==0){
            return result;
        }
        List<Map<String, Object>> userList= this.selectUserListForSimpleInfoByUserids( userids);

        if(userList==null||userList.size()==0){
            return result;
        }else{
            for (Map<String, Object> map : userList) {
                result.put((String) map.get("userid"),(String)map.get("username"));
            }
        }
        return result;
    }
    /**
     * 查询adm 用户的部门、部门领导、岗位，岗位名称，上级部门编号，上级领导，上上级领导
     * @param sysUser
     * @return
     */
    
    public List<Map<String,Object>> selectUserDeptPost(Map<String, Object> sysUser){
        return selectUserDeptPost(sysUser);
    }

    /**
     * 自定义查询，支持多表关联
     * @param page 分页条件
     * @param ew 一定要，并且必须加@Param("ew")注解
     * @param ext 如果xml中需要根据某些值进行特殊处理，可以通过这个进行传递，非必须，注解也可以不加
     * @return
     */
    public List<Map<String,Object>> selectListMapByWhere(IPage page, QueryWrapper ew, Map<String,Object> ext){
        return baseMapper.selectListMapByWhere(page,ew,ext);
    }

    public Map<String, Object> getDept(String deptid) {
        return getDept(deptid);
    }

    
    public Map<String, Object> getUserDeptPost(Map<String, Object> sysUser) {
        List<Map<String,Object>> dpus=this.selectUserDeptPost(sysUser);
        if(dpus!=null&&dpus.size()>0) {
            Map<String, Object> dpu = null;
            Optional<Map<String, Object>> o = dpus.stream().filter(i -> "1".equals(i.get("master"))).findFirst();
            if (o.isPresent()) {
                dpu = o.get();
            } else {
                dpu = dpus.get(0);
            }
            return dpu;
        }
        return null;
    }

    /** 请在此类添加自定义函数 */

}

